package com.zkdcloud.controller;

import com.xdja.interceptor.annotation.BeforeProcess;
import com.xdja.interceptor.bean.ResultBean;
import com.xdja.interceptor.exception.InvokeException;
import com.xdja.interceptor.util.ValidateUtils;
import com.zkdcloud.aspectInterceptor.advice.TokenAdvice;
import com.zkdcloud.domain.Admin;
import com.zkdcloud.domain.Business;
import com.zkdcloud.service.BusinessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.HTTP_SUCCESS;
import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.OPERATOR_SUCCESS;
import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.REQUEST_ADMIN;

/**
 * 商家controller
 *
 * @author zk
 * @since 2018/4/23
 */
@RestController
@RequestMapping(value = "/mg/business")
public class BusinessController {
    /**
     * static logger
     */
    private static Logger logger = LoggerFactory.getLogger(BusinessController.class);

    @Autowired
    private BusinessService businessService;
    /**
     * 获取商家详情
     *
     * @param id 商家id
     * @return resultBean
     */
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    public ResultBean get(@PathVariable("id")String id){
        return ResultBean.successResult("");
    }

    /**
     * 更新指定商家信息
     *
     * @param business business
     * @return resultBean
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ResultBean update(Business business) throws InvokeException {
        ValidateUtils.assertCondition(!StringUtils.isEmpty(business.getId()),"id必填");
        businessService.update(business);
        return ResultBean.successResult(OPERATOR_SUCCESS);
    }

    /**
     * 获取my business
     *
     * @return resultBean
     */
    @BeforeProcess(advice = TokenAdvice.class)
    @RequestMapping(value = "/my")
    public ResultBean getMyBusiness(Map<String,Object> map){
        Admin admin = (Admin) map.get(REQUEST_ADMIN);
        Business business = businessService.get(admin.getBusinessId());
        return ResultBean.successResult(HTTP_SUCCESS,business);
    }
}
